home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1B5T9R5 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  4.1 KB  |  119 lines

  1. package java.beans;
  2.  
  3. import java.lang.reflect.Method;
  4.  
  5. public class IndexedPropertyDescriptor extends PropertyDescriptor {
  6.    private Class indexedPropertyType;
  7.    private Method indexedReadMethod;
  8.    private Method indexedWriteMethod;
  9.  
  10.    IndexedPropertyDescriptor(PropertyDescriptor x, PropertyDescriptor y) {
  11.       super(x, y);
  12.       if (x instanceof IndexedPropertyDescriptor) {
  13.          IndexedPropertyDescriptor ix = (IndexedPropertyDescriptor)x;
  14.          this.indexedReadMethod = ix.indexedReadMethod;
  15.          this.indexedWriteMethod = ix.indexedWriteMethod;
  16.          this.indexedPropertyType = ix.indexedPropertyType;
  17.       }
  18.  
  19.       if (y instanceof IndexedPropertyDescriptor) {
  20.          IndexedPropertyDescriptor iy = (IndexedPropertyDescriptor)y;
  21.          if (iy.indexedReadMethod != null) {
  22.             this.indexedReadMethod = iy.indexedReadMethod;
  23.          }
  24.  
  25.          if (iy.indexedWriteMethod != null) {
  26.             this.indexedWriteMethod = iy.indexedWriteMethod;
  27.          }
  28.  
  29.          this.indexedPropertyType = iy.indexedPropertyType;
  30.       }
  31.  
  32.    }
  33.  
  34.    public IndexedPropertyDescriptor(String propertyName, Class beanClass) throws IntrospectionException {
  35.       this(propertyName, beanClass, "get" + capitalize(propertyName), "set" + capitalize(propertyName), "get" + capitalize(propertyName), "set" + capitalize(propertyName));
  36.    }
  37.  
  38.    public IndexedPropertyDescriptor(String propertyName, Class beanClass, String getterName, String setterName, String indexedGetterName, String indexedSetterName) throws IntrospectionException {
  39.       super(propertyName, beanClass, getterName, setterName);
  40.       this.indexedReadMethod = Introspector.findMethod(beanClass, indexedGetterName, 1);
  41.       this.indexedWriteMethod = Introspector.findMethod(beanClass, indexedSetterName, 2);
  42.       this.findIndexedPropertyType();
  43.    }
  44.  
  45.    public IndexedPropertyDescriptor(String propertyName, Method getter, Method setter, Method indexedGetter, Method indexedSetter) throws IntrospectionException {
  46.       super(propertyName, getter, setter);
  47.       this.indexedReadMethod = indexedGetter;
  48.       this.indexedWriteMethod = indexedSetter;
  49.       this.findIndexedPropertyType();
  50.    }
  51.  
  52.    private static String capitalize(String s) {
  53.       char[] chars = s.toCharArray();
  54.       chars[0] = Character.toUpperCase(chars[0]);
  55.       return new String(chars);
  56.    }
  57.  
  58.    private void findIndexedPropertyType() throws IntrospectionException {
  59.       try {
  60.          this.indexedPropertyType = null;
  61.          if (this.indexedReadMethod != null) {
  62.             Class[] params = this.indexedReadMethod.getParameterTypes();
  63.             if (params.length != 1) {
  64.                throw new IntrospectionException("bad indexed read method arg count");
  65.             }
  66.  
  67.             if (params[0] != Integer.TYPE) {
  68.                throw new IntrospectionException("non int index to indexed read method");
  69.             }
  70.  
  71.             this.indexedPropertyType = this.indexedReadMethod.getReturnType();
  72.             if (this.indexedPropertyType == Void.TYPE) {
  73.                throw new IntrospectionException("indexed read method returns void");
  74.             }
  75.          }
  76.  
  77.          if (this.indexedWriteMethod != null) {
  78.             Class[] params = this.indexedWriteMethod.getParameterTypes();
  79.             if (params.length != 2) {
  80.                throw new IntrospectionException("bad indexed write method arg count");
  81.             }
  82.  
  83.             if (params[0] != Integer.TYPE) {
  84.                throw new IntrospectionException("non int index to indexed write method");
  85.             }
  86.  
  87.             if (this.indexedPropertyType != null && this.indexedPropertyType != params[1]) {
  88.                throw new IntrospectionException("type mismatch between indexed read and indexed write methods");
  89.             }
  90.  
  91.             this.indexedPropertyType = params[1];
  92.          }
  93.  
  94.          if (this.indexedPropertyType == null) {
  95.             throw new IntrospectionException("no indexed getter or setter");
  96.          } else {
  97.             Class propertyType = ((PropertyDescriptor)this).getPropertyType();
  98.             if (propertyType != null && (!propertyType.isArray() || propertyType.getComponentType() != this.indexedPropertyType)) {
  99.                throw new IntrospectionException("type mismatch between indexed and non-indexed methods");
  100.             }
  101.          }
  102.       } catch (IntrospectionException var2) {
  103.          throw var2;
  104.       }
  105.    }
  106.  
  107.    public Class getIndexedPropertyType() {
  108.       return this.indexedPropertyType;
  109.    }
  110.  
  111.    public Method getIndexedReadMethod() {
  112.       return this.indexedReadMethod;
  113.    }
  114.  
  115.    public Method getIndexedWriteMethod() {
  116.       return this.indexedWriteMethod;
  117.    }
  118. }
  119.